<template>
  <div class="system-info">
    <!-- 面包屑 -->
    <div class="bread-crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>已结束流程</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="main-middle">
      <el-card class="box-card">
        <!-- 查询条件 -->
        <el-row class="table-selbox" v-if="btnSel">
          <!-- 查询条件表单 -->
          <el-form :inline="true" :model="formInline" >
            <!-- 流程名称 -->
            <el-form-item label="流程名称">
              <el-select
                v-model="formInline.flowId"
                filterable
                auto-complete="off"
                placeholder="请选择"
                style="width:150px;"
              >
                <el-option label="无" value></el-option>
                <!-- 循环所有角色 角色名称 角色ID -->
                <el-option
                  v-for="(item,index) in flowList"
                  :key="index"
                  :label="item.flowName"
                  :value="item.flowId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="发起人">
              <!-- 员工选择框子组件 -->
              <user-sel :users="users" @seluser="selUser"></user-sel>
            </el-form-item>
            <!-- 搜索 -->
            <el-form-item>
              <el-button icon="el-icon-search" @click="selHandle">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-row>
        <!-- 数据列表 -->
        <el-table
          :data="menusData"
          border
          style="width: 100%"
          height="395"
          ref="menusData"
        >
          <!-- 流程编号 -->
          <el-table-column :show-overflow-tooltip="true" prop="flowCode" label="流程编号" width="220"></el-table-column>
          <!-- 发起人 -->
          <el-table-column
            :show-overflow-tooltip="true"
            prop="flowOriginator"
            align="center"
            label="发起人"
            width="100"
          ></el-table-column>
          <!-- 申请时间 时间格式过滤 -->
          <el-table-column label="申请时间">
            <template slot-scope="scope">{{ scope.row.flowOriginatorTime | filterTime }}</template>
          </el-table-column>
          <!-- 操作列 -->
          <el-table-column
            label="管理"
            fixed="right"
            align="center"
            width="220"
            v-if="[btnDel,btnEdit].indexOf(true) > -1"
          >
            <template slot-scope="scope">
              <!-- 详细信息 -->
              <el-button size="mini" @click="formDatas(scope.row)" v-if="btnDel">详细信息</el-button>
              <!-- 审批历史 -->
              <el-button type="info" size="mini" @click="history(scope.row)" v-if="btnEdit">审批历史</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="table-pageblock">
          <!-- current-page 当前页数  page-size 每页显示数 total 总页数-->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[20,50,80,100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </el-card>
      <el-dialog title="审批记录" :visible.sync="modalVisible" width="600px">
        <div class="flowLog">
          <el-table
            :data="historyLog"
            border
            style="width: 100%">
            <!-- 处理人 -->
            <el-table-column prop="createUserId" label="处理人" align="center" width="100"></el-table-column>
            <!-- 处理日期 时间格式过滤 -->
            <el-table-column label="处理时间" align="center" width="150">
              <template slot-scope="scope">{{ scope.row.createTime | filterTime }}</template>
            </el-table-column>
            <el-table-column align="center" label="结果" width="60">
              <template slot-scope="scope">
                <!-- 处理结果 1.发起 2.通过 3.驳回 4.取消 -->
                <span v-if="scope.row.result === 1">
                  发起
                </span>
                <span v-if="scope.row.result === 2">
                  通过
                </span>
                <span v-if="scope.row.result === 3">
                  驳回
                </span>
                <span v-if="scope.row.result === 4">
                  取消
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="note"
              label="审批说明">
            </el-table-column>
          </el-table>
        </div>
        <!-- <node-data ref="nodeFlow" :nodeData="nodeData"></node-data> -->
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 引入时间格式化插件
import moment from 'moment';
// 引入员工选择组件
import userSel from '@/components/UserSel/index.vue';

import NodeData from '@/components/NodeData';

export default {
  // 注册组件
  components: {
    userSel,
    NodeData
  },
  // 父组件传值子组件
  props: {
    // 按钮级权限
    btns: {
      type: Object
    }
  },

  // 无白屏刷新方法注入
  inject: ['reload'],

  // 监听
  watch: {
    // 按钮权限数据
    btns (val, oldVal) {
      // 刷新当前页
      this.reload();
    }
  },

  // 实例创建后执行
  created () {
    // 按钮级授权
    this.authorization(this.$store.state.btns);

    this.getFlowList();

    // 获取所有员工
    this.getUsers();

    // 加载分页数据
    this.getmenusDataByPage();
  },

  // 过滤器
  filters: {
    // 时间显示过滤
    filterTime (value) {
      return moment(value).format('YYYY-MM-DD HH:mm');
    }
  },

  // 当前页面数据
  data () {
    return {
      // 流程图预览数据
      nodeData: {},
      modalVisible: false,
      flowList: [],

      historyLog: [],
      // 屏蔽重复提交
      isSubmit: false,
      // 按钮显示隐藏  - 默认值
      btnSel: true,
      btnDel: true,
      btnUpload: true,
      btnDown: true,
      btnAdd: true,
      btnEdit: true,

      // 获取所有用户
      users: [],
      // 用户选择框子组件选中值
      getUser: [],

      // 搜索条件
      formInline: {
        flowId: '',
        flowOriginator: ''
      },

      // 列表数据
      menusData: [{}],
      // 当前页
      currentPage: 1,
      // 当前数
      pageSize: 20,
      // 总条目数
      total: 20
    };
  },
  methods: {
    formDatas: function (row) {
      let params = {
        flowId: row.flowId
      };
      this.$http.post('api/findFlowRouter', params).then(res => {
        let { code } = res;
        // 判断Code状态
        if (code === 0) {
          this.$router.push({
            path: res.datas,
            query: {
              node: 2,
              datas: row
            }
          });
        }
      }).catch(err => {
        if (err === undefined) {
          this.$message({
            message: '服务器响应异常！',
            type: 'error'
          });
          return false;
        }
      });
    },
    // 按钮级授权
    authorization (data) {
      for (let i in data) {
        if (data['sel'] === 0) {
          this.btnSel = false;
        }
        if (data['add'] === 0) {
          this.btnAdd = false;
        }
        if (data['del'] === 0) {
          this.btnDel = false;
        }
        if (data['upload'] === 0) {
          this.btnUpload = false;
        }
        if (data['down'] === 0) {
          this.btnDown = false;
        }
        if (data['edit'] === 0) {
          this.btnEdit = false;
        }
        return i;
      }
    },

    getCls (data) {
      this.drawer = data;
    },
    // 获取所有流程数据
    getFlowList: function () {
      this.$http.raw('api/findAllFlow').then(res => {
        let { code, datas } = res;
        if (code === 0) {
          this.flowList = datas;
        }
      }).catch(err => {
        if (err === undefined) {
          this.$message({
            message: '服务器响应异常！',
            type: 'error'
          });
          return false;
        }
      });
    },

    // 审批历史
    history: function (row) {
      let params = {
        flowCode: row.flowCode
      };

      this.$http.post('api/findWorkResultList', params).then(res => {
        let { code } = res;
        // 判断Code状态
        if (code === 0) {
          let currentNode = row.flowCurrentNode;

          let flowJson = JSON.parse(row.flowJson);
          flowJson.nodes.map(item => {
            if (item.id === currentNode) {
              item.color = '#FF0000';
            }
          });

          // 流程图预览
          this.modalVisible = true;
          this.nodeData = flowJson;
          this.historyLog = res.datas;
        }
      }).catch(err => {
        if (err === undefined) {
          this.$message({
            message: '服务器响应异常！',
            type: 'error'
          });
          return false;
        }
      });
    },
    // 获取所有用户
    getUsers () {
      let _this = this;
      let params = {
        status: ''
      };
      _this.$http.post('api/findAllBaseUsers', params).then(res => {
        let { code, datas } = res;
        // 判断Code状态
        if (code === 0) {
          _this.users = datas;
        }
      }).catch(err => {
        if (err === undefined) {
          _this.$message({
            message: '服务器响应异常！',
            type: 'error'
          });
          return false;
        }
      });
    },

    // 获取用户选择框选中值
    selUser (data) {
      this.getUser = data;
    },

    // 查询
    selHandle: function () {
      // 收集请求数据
      let params = {
        flowId: this.formInline.flowId === null ? '' : this.formInline.flowId,
        flowOriginator: this.getUser[0],
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      // 根据公司ID请求对应分页数据结果
      this.$http.raw('api/findEndOrder', params).then(res => {
        let { code, datas } = res;
        if (code === 0) {
          // 总页数
          this.total = datas.total;
          // 数据结果集
          this.menusData = datas.list;
          // 解决删除当前页数据后仍留在那一页 不跳回上一页的bug
          if (!datas.list.length && this.currentPage !== 1) {
            // 当前页减一
            this.currentPage -= 1;
            // 调用自己
            this.getmenusDataByPage();
          }
        }
      }).catch(err => {
        if (err === undefined) {
          this.$message({
            message: '服务器响应异常！',
            type: 'error'
          });
          return false;
        }
      });
    },

    // 刷新页面数据
    getmenusDataByPage () {
      // 收集请求参数
      let params = {
        flowId: '',
        flowOriginator: '',
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      this.$http.raw('api/findEndOrder', params).then(res => {
        let { code, datas } = res;
        // 判断Code状态
        if (code === 0) {
          // 总页数
          this.total = datas.total;
          // 数据结果集
          this.menusData = datas.list;
          // 解决删除当前页数据后仍留在那一页 不跳回上一页的bug
          if (!datas.list.length && this.currentPage !== 1) {
            // 当前页减一
            this.currentPage -= 1;
            // 调用自己
            this.getmenusDataByPage();
          }
        }
      }).catch(err => {
        if (err === undefined) {
          this.$message({
            message: '服务器响应异常！',
            type: 'error'
          });
          return false;
        }
      });
    },

    // 每页显示的条数改变
    handleSizeChange (val) {
      this.pageSize = val;
      // 调用刷新页面
      this.getmenusDataByPage();
    },
    // 当前页码
    handleCurrentChange (val) {
      this.currentPage = val;
      // 调用
      this.getmenusDataByPage();
    }
  }
};
</script>

<style lang='scss'>
@import './index.scss';
</style>
